package com.gujiangbo.lock;

/**
 * @Author: gujiangbo.co
 * @Contact: 646832012@qq.com
 * @Date: 2022/3/14 20:29
 * @Version: 1.0
 * @Description: 线程之间的等待与唤醒，wait/notify 必须要与同步代码块一起使用，否则没有办法做到锁对象的等待与唤醒。会报非法监视器异常。
 */
public class JavaNotifyWait {

    private static final Object objectlock = new Object();

    public static void main(String[] args) {
        new Thread(() -> {

            synchronized (objectlock) {
                System.out.println(Thread.currentThread().getName() + "\t" + " coming in ");
                try {
                    objectlock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println(Thread.currentThread().getName() + "\t" + " 被唤醒.....");
            }
        }, "A").start();


        new Thread(() -> {

            synchronized (objectlock) {
                System.out.println(Thread.currentThread().getName() + "\t" + " coming in ");
                objectlock.notify();
            }
        }, "B").start();
    }
}
